<template>
	<div class="module">
		<div class="container">
		<div class="top-form">
			<el-button type="primary" @click="onAdd">新增</el-button>
		</div>
        <STtable size="mini" :defaultExpandAll="true" :data.sync="tableData" @doRefresh="fetchData" :showPagination="false" row-key="id"  :field-list="tables">
            <template  slot="col-status" slot-scope="scope">{{(dicts.yesOrNo[scope.row.status])? dicts.sysRoleStatus[scope.row.status]:"禁用"}}</template>
            <template  slot="col-ishow" slot-scope="scope">{{(dicts.yesOrNo[scope.row.ishow])? dicts.yesOrNo[scope.row.ishow]:"禁用"}}</template>
        	<template slot="col-issys" slot-scope="scope">{{(dicts.yesOrNo[scope.row.issys])? dicts.yesOrNo[scope.row.issys]:"禁用"}}</template>
            <template slot="col-btn" slot-scope="scope">
                <el-button size="mini" @click="onEdit(scope.row)">编辑</el-button>
                <el-button size="mini" type="warning" @click="onCopy(scope.row)">复制</el-button>
                <el-button size="mini" type="danger" @click="onDelete(scope.row)">删除</el-button>
            </template>
        </STtable>
	</div>
	<!-- 编辑弹出框 -->
	<el-dialog :title="page.popTitle" center :visible.sync="page.dialogVisible" :width="page.popWidth">
		<div v-if="page.pop == 1 || page.pop == 3" class="pop_box">
			<STform :ref-obj.sync="formInfo.ref" :data="formInfo.data" :field-list="formInfo.fieldList" :rules="formInfo.rules"
				 :listTypeInfo="dictList" :label-width="formInfo.labelWidth" @handleEvent="handleFormEvent"></STform>
			<div class="dialog-footer">
				<el-button @click="hidePop">取 消</el-button>
				<el-button type="primary" @click="doSubmit">确 定</el-button>
			</div>
		</div>
		<div v-if="page.pop == 2"  class="pop_box">
			<div class="pop_concent">删除不可恢复，是否确定删除？</div>
			<div slot="footer" class="dialog-footer">
				<el-button @click="hidePop">取 消</el-button>
				<el-button type="primary" @click="deleteRow">确 定</el-button>
			</div>
		</div>
	</el-dialog>
</div>
</template>

<script>
	import STform from '@/components/common/STform'
	import STtable from '@/components/common/STtable'
	import modAPI from '@/api/sys/module.js'
	import consts from './consts.js'
	import {mapState} from 'vuex' //本地取信息
	export default {
		components: {STform,STtable},
		data() {
			return {
				details: {},
				tableData:[],
				page: {
					popWidth: '720px',
					popTitle: null,
					dialogVisible: false,
					pop: 0
				},
				tables:consts.tables,
				formInfo: { // 表格数据
					ref: null,
					data: consts.fieldData(null),
					rules: consts.rules,
					labelWidth: '120px',
					fieldList: consts.fieldList
				}
			}
		},
		async created() {
            await this.fetchData();
			this.loading = false;
		},
		computed: {
			dictList() {
				return this.$store.getters["dictSrv/list"];
			},
			...mapState({
		        dicts: (state) => {
		          return state.dictSrv.data;
		        }
		    })
		},
		methods: {
            async fetchData() {
            	const tree = await modAPI.getTree();
            	if (tree) {
            		this.tableData = tree;
            	}
            	this.loading = false;
            },
			async onDelete(data) {
				this.details = data;
				this.showPop(2);
			},
			async deleteRow() {
				const osjon = await modAPI.doDelete({id:this.details.id});
				if(osjon){
					this.fetchData();
					this.hidePop();
				}
			},
			onAdd() {
				this.formInfo.ref && this.formInfo.ref.resetFields();
				this.formInfo.data = consts.fieldData(null);
				this.showPop(1);
			},
			async onEdit(data) {
				this.formInfo.ref && this.formInfo.ref.resetFields();
				data = JSON.parse(JSON.stringify(data));
				this.formInfo.data = consts.fieldData(data);
				this.showPop(3);
			},
            async onCopy(data) {
                this.formInfo.ref && this.formInfo.ref.resetFields();
                data = JSON.parse(JSON.stringify(data));
                data.id = 0;//复制模块
                this.formInfo.data = consts.fieldData(data);
                this.showPop(3);
            },
			async doSubmit() {
				this.formInfo.ref.validate(async (valid) => {
					if (valid) {
                        this.formInfo.data.rank = parseInt(this.formInfo.data.rank);
						(!this.formInfo.data.pid) && (this.formInfo.data.pid = 0);
						const data = JSON.parse(JSON.stringify(this.formInfo.data))
						var res = await modAPI.doSave(data);
						if (res) {//重新加载数据
							await this.fetchData();
                            this.hidePop();
						}
					} else {
						return false;
					}
				});
			},
			handleFormEvent() {
			},
			handleEvent(type, data) {
                if (type == "tableCheck") {
                    this.multipleSelection = [];
                    for (const index in data) {
                    	this.multipleSelection.push(data[index].styleId);
                    }
                }
			},
			showPop(pop) {
				this.page.popWidth = "720px";
				if (pop == 1 || pop == 3) {
					this.page.popTitle = ((pop == 1)? '新增':'编辑') + '权限模块';
				} else  {
					this.page.popTitle = '删除权限模块';
					this.page.popWidth = "300px";
				}
				this.page.pop = pop;
				this.page.dialogVisible = true;
			},
			hidePop() {
				this.page.dialogVisible = false;
				this.page.pop = 0;
			}
		}
	}
</script>

<style scoped>
	.pop_box .pop_left{
		display: inline-block;
		vertical-align: top;
		width: calc( 100% - 244px);
	}
	.pop_box .pop_right{
		display: inline-block;
		vertical-align: top;
		width: 240px;
		height: 500px;
		position: relative;

		overflow:scroll;
	}
	.el-tree{
		position: absolute;
	}
</style>
